package com.mengwen.backstage.controller.partner;

import com.mengwen.backstage.config.QiniuConfig;
import com.mengwen.backstage.config.WeChatConfig;
import com.mengwen.backstage.pojo.AccessToken;
import com.mengwen.backstage.pojo.JsSdkDto;
import com.mengwen.backstage.service.FileService;
import com.mengwen.backstage.util.GetLastAccessTokenUtil;
import com.mengwen.backstage.util.JsSdkUtil;
import com.mengwen.backstage.util.JwtUtil;
import com.mengwen.backstage.util.QRCodeUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author LiangYonghui
 * @date 2020/3/25 16:38
 * @description  有关二维码
 */
@Controller
@Slf4j
public class ShareController {
    @Autowired
    WeChatConfig weChatConfig;

    @Autowired
    GetLastAccessTokenUtil getLastAccessTokenUtil;

    @Autowired
    FileService fileService;

    @Autowired
    QiniuConfig qiniuConfig;


    /**
     * 去分享页面
     * @param partnerToken
     * @param modelview
     * @return
     */
    @RequestMapping("/share")
    public String sendOpenid(@RequestParam("partner_token") String partnerToken, Model modelview){
        log.info("测试分享页面");
        Claims claims = JwtUtil.checkJWT(partnerToken);
        String role = (String) claims.get("role");
        String partnerUnionid = (String) claims.get("partnerUnionid");
        System.out.println(partnerUnionid);
        if ("partner".equals(role) && (null != partnerUnionid)) {
            modelview.addAttribute("partner_token",partnerToken);
            modelview.addAttribute("partnerUnionid",partnerUnionid);
        return "share_style";
        }else {
            return "login_door";
        }
    }


    /**
     *
     * 获取和接口对接的签名等各种参数
     * @param request
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/get_parameter")
    @ResponseBody
    public JsSdkDto getParameter(HttpServletRequest request) throws Exception {
        String appid = weChatConfig.getAppId();
        AccessToken accessToken = getLastAccessTokenUtil.getAccessToken();
        String wxOaRedirectUrl = weChatConfig.getWxOaRedirectUrl();
        String url = wxOaRedirectUrl + "/share";
        System.out.println(url);
        Map<String, String> sign = JsSdkUtil.sign(url, accessToken.getToken(), appid);
        String nonceStr = sign.get("nonceStr");
        String signature = sign.get("signature");
        String timestamp = sign.get("timestamp");
        return new JsSdkDto(appid, timestamp, nonceStr, signature);
    }


    /**
     * 扫描进入小程序的页面
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/smapp")
    public String h(HttpServletRequest request,Model model) throws Exception {
        log.info("进入smapp");
        // 获取分享用户的unionid:
        String unionid = request.getParameter("unionid");
        log.info(unionid);
        // 拼接小程序的路径：
        String smappUrl = weChatConfig.getSmappUrl();
        String text = smappUrl + "/" + unionid;
        // 指定二维码logo以及生成的图片存放路径生成二维码，并返回二维码名称：
        String fileName = QRCodeUtil.encode(text, qiniuConfig.getLocalImgPath()+ "/logo.jpg", qiniuConfig.getLocalImgPath(),  true);

        String path = qiniuConfig.getLocalImgPath() + "/" + fileName;

        // 存入七牛云，并拿到七牛云的url，放进imgurl
        String imgurl = fileService.uploadImageWithPath(path);

        // 查看是否上传成功：
        log.info("传到七牛云的图片为路径为：" + imgurl);
        String substring = imgurl.substring(path.lastIndexOf('/') + 1);
        if("null".equals(substring)) {
            log.info("上传图片失败了：" + fileName);
        }else {  // 上传成功，删除本地目录下的二维码图片，防止项目臃肿
            boolean b = fileService.deleteImageWithFileName(fileName);
            if (b) {
                log.info("删除本地二维码图片：" + fileName + "成功");
            }else {
                log.info("删除本地二维码图片：" + fileName + "失败");
            }
        }

        // 将信息放入分享页面：
        model.addAttribute("imgurl",imgurl);
        return "small_app";
    }


    /**
     * 扫描进入pc端的页面
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/pc")
    public String z(HttpServletRequest request,Model model) throws Exception {
        log.info("进入pc_code");
        // 获取分享用户的unionid:
        String unionid = request.getParameter("unionid");
        log.info(unionid);
        // 拼接小程序的路径：
        String smappUrl = weChatConfig.getSmappUrl();
        String text = smappUrl + "/" + unionid;
        // 生成二维码，并返回二维码名称：
        String fileName = QRCodeUtil.encode(text, "D:\\myProjects\\backstage\\src\\main\\resources\\static\\img\\logo.jpg", "D:\\myProjects\\backstage\\src\\main\\resources\\static\\img",  true);
        // 将信息放入分享页面：
        String imgurl = "https://www.mengwenquda.cn/getImage?num=temp14";
        // 存入七牛云，并拿到七牛云的url，放进imgurl
        // imgurl = getfrom7niuyun();
        model.addAttribute("imgurl",imgurl);
        return "pc_code";

    }



}
